package com.ciko.code.http;

import com.ciko.code.constant.Constants;
import com.ciko.code.utils.Base64;
import com.ciko.code.utils.LogUtils;
import com.loopj.android.http.RequestParams;
import com.mywork.WorkUIKit;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.ConcurrentHashMap;

public abstract class BaseRequest extends RequestParams {

    private static final String TAG = "BaseRequest";

    protected long timeStamp = System.currentTimeMillis();

    protected BaseRequest() {

    }

    protected void init() {
        put("account", WorkUIKit.getUser().getAccount());
        put("timeStamp", getTimeStamp());
        put("isNew", "y");
        putClientType();
        putAccountType();
        putEnterpriseId();
    }

    protected void initUser() {
        put("account", WorkUIKit.getUser().getAccount());
        put("timeStamp", getTimeStamp());
        //FIXME userNo
//        put("userNo", WorkUIKit.getUser().getUserNo());
        putClientType();
        putAccountType();
        putEnterpriseId();
    }

    protected void initNoEnId() {
        put("account", WorkUIKit.getAccount());
        put("timeStamp", getTimeStamp());
        putClientType();
        putAccountType();
    }

    protected void putAccountType() {
        put("accountType", Constants.ACCOUNT_TYPE);
    }

    protected void putClientType() {
        put("clientType", Constants.CLIENTTYPE);
    }

    protected void putTimeStamp() {
        put("timeStamp", String.valueOf(timeStamp));
    }

    protected void putEnterpriseId() {
        put("enterpriseId", Constants.enterprise_num);
    }

    protected void putCid(Object paramString) {
//        Log.e(TAG, App.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString);
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString.toString()));
    }
    protected void putCidUser(Object paramString) {
//        Log.e(TAG, App.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString);
        //FIXME userNo
        //put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + WorkUIKit.getUser().getUserNo() + paramString.toString()));
    }

    protected void putCid() {
//        Log.e(TAG, App.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId());
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()));
    }

    protected void putCidUser() {
//        Log.e(TAG, WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId());
        //FIXME userNo
        //put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + WorkUIKit.getUser().getUserNo()));
    }

    protected void putCidWorkMeeting(String paramString) {
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString));

    }

    protected void putCidWorkMeetingDetail(String paramString) {
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString));

    }
    //外勤申请
    protected void addWorkOutsideInfo(int nextUserId, String nextUserName, String json) {
        init();
        put("nextUserId",nextUserId);
        put("nextUserName",nextUserName);
        put("json",json);
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ nextUserId+nextUserName));

    }
    //外勤信息列表
    protected void qryWorkOutsideInfoList(int type, int pageIndex, int pageCount) {
        init();
        put("type",type);
        put("pageIndex",pageIndex);
        put("pageCount",pageCount);
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ type+pageIndex+pageCount));

    }
    //外勤信息详情
    protected void qryWorkOutsideInfos(int outsideId) {
        init();
        put("outsideId",outsideId);

        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ outsideId));

    }
    //外勤审批同意流程
    protected void consentOutsideApply(int outsideId, String direction, int isend,int nextUserId,String nextUserName) {
        init();

        put("outsideId",outsideId);
        put("direction",direction);
        put("isend",isend);
        put("nextUserId",nextUserId);
        put("nextUserName",nextUserName);

        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ outsideId+direction+isend));

    }
    //外勤审批否决流程
    protected void refuseOutsideApply(int outsideId, String direction) {
        init();

        put("outsideId",outsideId);
        put("direction",direction);

        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ outsideId+direction));

    }
    //外勤签到
    protected void signWorkOutsideInfos(int outsideId, String actualAddr,String actualLat,String actualLon) {
        init();

        put("outsideId",outsideId);
        put("actualAddr",actualAddr);
        put("actualLat",actualLat);
        put("actualLon",actualLon);

        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ outsideId+actualAddr+actualLat+actualLon));

    }
    //拜访反馈
    protected void signfeedbackInfos(int outsideId, String feedback) {
        init();

        put("outsideId",outsideId);
        put("feedback",feedback);

        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId()+ outsideId+feedback));

    }

    //我的外勤签到
    protected void putMyFieldWorkParmas(long lastTime,int length) {
        put("enterpriseId",getEnterpriseId());
        put("account",WorkUIKit.getUser().getAccount());
        put("lastCreateTime",lastTime);
        put("length",length+"");
        put("cid", cid(getEnterpriseId()+WorkUIKit.getUser().getAccount()+ getTimeStamp() + length));

    }
    //拜访反馈参数
    protected void putMyFeedBackParmas(String outsideId,String feedback) {
        put("enterpriseId",getEnterpriseId());
        put("account",WorkUIKit.getUser().getAccount());
        put("outsideId",outsideId);
        put("feedback",feedback);
        put("cid", cid(getEnterpriseId()+WorkUIKit.getUser().getAccount()+outsideId+ feedback ));
    }
    //领导指导参数
    protected void putGuideParmas(String outsideId,String direction) {
        put("enterpriseId",getEnterpriseId());
        put("account",WorkUIKit.getUser().getAccount());
        put("outsideId",outsideId);
        put("direction",direction);
        put("cid", cid(getEnterpriseId()+WorkUIKit.getUser().getAccount()+outsideId+ direction ));
    }

    protected void putCidNoEnId(String paramString) {
//        Log.e(TAG, App.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + getEnterpriseId() + paramString);
        put("cid", cid(WorkUIKit.getUser().getAccount() + getAccountType() + getClientType() + getTimeStamp() + paramString));
    }

    protected String getAccountType() {
        return Constants.ACCOUNT_TYPE;
    }

    protected String getClientType() {
        return Constants.CLIENTTYPE;
    }

    protected String getTimeStamp() {
        return String.valueOf(timeStamp);
    }

    protected String getEnterpriseId() {
        return Constants.enterprise_num;
    }


    public abstract String getUrl();

    public void put(String key, Object value) {
        if (value != null) {
            super.put(key, value.toString());
        }
    }

    protected String cid(String value) {
        if (WorkUIKit.getUser() != null) {
            LogUtils.e("自己cid = " + WorkUIKit.getUser().getKey());
            return Base64.base64Encode(value, Constants.SERVER_ENCRYPTKEY, "UTF-8");
        } else {
            LogUtils.e(Constants.SERVER_ENCRYPTKEY);
            return Base64.base64Encode(value, Constants.SERVER_ENCRYPTKEY, "UTF-8");
        }
    }

    public abstract Class<?> getResponseClazz();

    public ConcurrentHashMap<String, String> getUrlParam() {
        return urlParams;
    }

    protected static String getUTF8String(String city) {
        String cityUTF8 = "";
        try {
            cityUTF8 = URLEncoder.encode(city, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return cityUTF8;
    }

}
